const imageTypes = [
	"png",
	"jpg",
	"jpeg",
	"bmp",
	"gif",
	"webp",
	"psd",
	"svg",
	"tiff",
];
const videoTypes = [
	"avi",
	"wmv",
	"mpeg",
	"mp4",
	"m4v",
	"mov",
	"asf",
	"flv",
	"f4v",
	"rmvb",
	"rm",
	"3gp",
	"vob",
	"video",
];

let util = {
	/**
	 * 根据文件后缀判断文件类型，image，video，file
	 */
	fileType: function(fileName) {
		const index = fileName.lastIndexOf(".");
		const ext = fileName.substring(index + 1);

		if (imageTypes.indexOf(ext.toLowerCase()) >= 0) {
			return "image";
		} else if (videoTypes.indexOf(ext.toLowerCase()) >= 0) {
			return "video";
		} else {
			return "file";
		}
	},
	filePathToUrl(path) {
		if (!path) {
			return "";
		}
		let filePathPre = process.env.VUE_APP_FILE_PATH_PRE;

		let fileRoutePre = process.env.VUE_APP_FILE_ROUTE_PRE;
		path = path.replace(filePathPre, fileRoutePre);

		return path;
	},
	//获取唯一编码 len指定长度
	getUuid(len, radix) {
		var chars =
			"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
				""
			);
		var uuid = [],
			i;
		radix = radix || chars.length;

		if (len) {
			// Compact form
			for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
		} else {
			// rfc4122, version 4 form
			var r;
			// rfc4122 requires these characters
			//   uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
			uuid[14] = "4";

			// Fill in random data.  At i==19 set the high bits of clock sequence as
			// per rfc4122, sec. 4.1.5
			for (i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | (Math.random() * 16);
					uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
				}
			}
		}

		return uuid.join("");
	},
	numberFormatNoFixed(num, decimal) {
		num = num.toString();
		let index = num.indexOf('.');
		if (index !== -1) {
			num = num.substring(0, decimal + index + 1);
		} else {
			num = num.substring(0);
		}
		const formatWithRegex = (number) => {
			return !(number + '').includes('.') ? // 就是说1-3位后面一定要匹配3位
				(number + '').replace(/\d{1,7}(?=(\d{3})+$)/g, (match) => {
					return match;
				}) :
				(number + '').replace(/\d{1,7}(?=(\d{3})+(\.))/g, (match) => {
					return match;
				});
		};
		return formatWithRegex(parseFloat(num).toFixed(decimal));
		// const number = parseFloat(parseFloat(value));
		// console.log("value", number);

		// const dem = Math.pow(10, decimals);
		// console.log(Math.floor(number * dem) / dem);
		// return (Math.floor(number * dem) / dem).toFixed(decimals);
	},
};

export default util;